var dataUrl = null;
var corpNum = 1;
$(function () {
    // $('#zoneDiv li').addClass('parent_li');
    // 绑定取消按钮事件
    $(".cancel").click(function () {
        window.location.reload();
    });
    $(document).click(function(){
        $("#epsDiv ul li").children("ul,li").slideUp(0);
        $("#zoneDiv ul li").children("ul").slideUp(0);
    });
/*    $("body").click( function() {
        $("#epsDiv ul li").children("ul,li").slideUp(0);
        $("#zoneDiv ul li").children("ul").slideUp(0);
    });*/
    $("form").validate({
        submitHandler: function (form) {// 必须写在验证前面，否则无法ajax提交
            //jquery获取复选框值
            if (!checkForm()) {
                return;
            }
            var num = 1;
            var corpPropCorpIds = "";
            $(".corp-prop").each(function () {
                if($("#corpPropId" + num).val()!=undefined&&$("#corpPropId" + num).val()!=""){
                    corpPropCorpIds += ($("#corpPropId" + num).val() + "-" + $("#corpId" + num).val() + ",");
                }
                num++;
            });
            $("#corpPropCorpIds").val(corpPropCorpIds);
            // var chk_value =[];//定义一个数组
            var index = layer.load(1, {shade: false});
            $("#submitButton").attr("disabled", "disabled");
            $(form).ajaxSubmit({
                dataType: "text",
                success: function (data) {
                    $("#submitButton").removeAttr("disabled");
                    layer.close(index);
                    if (data == "success") {
                        // layer.confirm('添加成功!是否关闭窗口?', function (index) {
                        layer.msg("添加成功！");
                        // $('#form')[0].reset();
                        window.location.reload();
                        return false;
                        // });
                    } else {
                        layer.alert('添加失败！', 3);
                    }
                }
            });
        },
        errorPlacement: function (error, element) {// 自定义提示错误位置
            $(".l_err").css('display', 'block');
            // element.css('border','3px solid #FFCCCC');
            $(".l_err").html(error.html());
        },
        success: function (label) {// 验证通过后
            $(".l_err").css('display', 'none');
        }
    });
    $("#epsDiv ul li ").click(function () {
        $("#zoneDiv ul li").children("ul").slideUp(0);

        if (event.stopPropagation) {
            // 针对 Mozilla 和 Opera
            event.stopPropagation();
        }else if (window.event) {
            // 针对 IE
            window.event.cancelBubble = true;
        }

        var thisSpan = $(this);
        // $(this).children("a").children("i").css("menu-icon-right")=='none'
        if ($(this).children('ul').css('display') == 'none') {
            $(this).children("a").children("i").removeClass("menu-icon-down");
            $(this).children("a").children("i").addClass("menu-icon-right");

            $(this).siblings().children("a").children("i").addClass("menu-icon-right");
            // $(".content-left-submenu ul li a i").prev().removeClass("menu-icon-right");
            // $("ul", this).prev("i").addClass("menu-icon-down");
            $(this).children("ul").slideDown("fast");
            $(this).siblings().children("ul").slideUp("fast");
        } else {
            $(this).children("a").children("i").removeClass("menu-icon-right");
            $(this).children("a").children("i").addClass("menu-icon-down");
            $(this).children("ul").slideUp("fast");
        }
    });
    $("#zoneDiv ul li ").click(function () {
        $("#epsDiv ul li").children("ul,li").slideUp(0);
        if (event.stopPropagation) {
            // 针对 Mozilla 和 Opera
            event.stopPropagation();
        }else if (window.event) {
            // 针对 IE
            window.event.cancelBubble = true;
        }
        var thisSpan = $(this);
        // $(this).children("a").children("i").css("menu-icon-right")=='none'
        if ($(this).children('ul').css('display') == 'none') {
            $(this).children("a").children("i").removeClass("menu-icon-down");
            $(this).children("a").children("i").addClass("menu-icon-right");

            $(this).siblings().children("a").children("i").addClass("menu-icon-right");
            // $(".content-left-submenu ul li a i").prev().removeClass("menu-icon-right");
            // $("ul", this).prev("i").addClass("menu-icon-down");
            $(this).children("ul").slideDown("fast");
            $(this).siblings().children("ul").slideUp("fast");
        } else {
            $(this).children("a").children("i").removeClass("menu-icon-right");
            $(this).children("a").children("i").addClass("menu-icon-down");
            $(this).children("ul").slideUp("fast");
        }
    });
    init();
});

function init() {
    dataUrl ="/modules";
    // dataUrl = $("#dataUrl").val();
    getProjectTypeList();
    getProjectDegreeList();
    // getProjectZoneList();
    getProjectCheckerList();
    getCorpProp(1);
    $("#epsDiv ul li").children("ul").slideUp();
    $("#zoneDiv ul li").children("ul").slideUp();
}

function checkForm() {
    if ($("#caption").val() == '') {
        layer.msg('项目名称不能为空');
        return false;
    }
    if ($("#cap").val() == '') {
        layer.msg('项目简称不能为空');
        return false;
    }
    /* if ($("#projectCode").val() == '') {
         layer.msg('项目编号不能为空');
         return false;
     }*/
    /* if ($("#proTypeId").val() == '') {
     layer.msg('项目类型不能为空');
     return false;
     }
     if ($("#proDegreeId").val() == '') {
     layer.msg('电压等级不能为空');
     return false;
     }*/
    if ($("#stime").val() == '') {
        layer.msg('开始时间不能为空');
        return false;
    }
    if ($("#epsId").val() == '') {
        layer.msg('请选择EPS');
        return false;
    }

    if ($("#etime").val() == '') {
        layer.msg('结束时间不能为空');
        return false;
    }
    /*  if ($("#closeTime").val() == '') {
          layer.msg('关闭时间不能为空');
          return false;
      }*/
    /*if ($("#contactInfo").val() == '') {
        layer.msg('联系方式不能为空');
        return false;
    }*/
    /* if ($("#zoneId").val() == '') {
         layer.msg('请选择所在地区');
         return false;
     }*/
    if ($("#checker").val() == '') {
        layer.msg('请选择审核人');
        return false;
    }
    return true;
}

function addXZDW() {
    corpNum++;
    var html = "<div id='corpPropDiv" + corpNum + "'>" +
        "<div style='height:0px;font-size:0;clear:both;'  onclick='deleteXZDW(" + corpNum + ")'>" +
        "<img src='../../images/delete-icon.png' style='width: 20px;height: 20px;margin-top: 17px;margin-left: 19px;'/>" +
        "</div>" +


        "  <div class=\"form-group-div form-layer-input clearfix\">" +
        "                            <label class=\"control-label2\">单位性质：</label>" +
        "                            <select name=\"corpPropId\" id=\"corpPropId" + corpNum + "\"  class=\"corp-prop input-medium2 form-control m-b\" " + " onchange=\"getCorpList(this.options[this.options.selectedIndex].value," + corpNum + ")\"" +
        "style=\"text-align: center;height: 36px\">" +
        // "                                <option value=\"2\">建管单位</option>\n" +
        // "                                <option value=\"5\">施工单位</option>\n" +
        // "                                <option value=\"4\">监理单位</option>\n" +
        "                            </select>" +
        "                        </div>" +
        "                        <div class=\"form-group-div form-layer-input clearfix\">" +
        "                            <label class=\"control-label2\">单位名称：</label>" +
        " <select name=\"corpId\"  id=\"corpId" + corpNum + "\" class=\"input-medium2 form-control m-b corp-name\"" +
        " style=\"text-align: center;height: 36px\">" +
        "</select>" +
        " </div>" +
        " </div>"
    ;
    $("#addCorp").before(html);
    // $("#corpPropId" + corpNum).trigger('change');
    getCorpProp(corpNum);

}

//删除性质单位组
function deleteXZDW(num) {
    $("#corpPropDiv" + num).remove();
}

//项目类型
function getProjectTypeList() {
    var url = dataUrl + "/basic/pro_type/all"+ "?newDate=" + (new Date().getTime()+Math.ceil(Math.random()*10));
    $.ajax({
        url: url,
        method: "get",
        dataType: "json",
        // crossDomain: true ,
        async: true,
        cache: false,
        crossDomain: true == !(document.all),
        success: function (result) {
            var html = "";
            // html += "<option value=''>------请选择------</option>";
            if (result != null) {
                for (var i = 0; i < result.length; i++) {
                    html += "<option value='" + result[i].id + "'>" + (result[i].caption) + "</option>";
                }
                $("#proTypeId").html(html);
            } else {
                layer.msg("获取列表信息错误，请联系管理员！");
            }
        }
    });
}

//电压
function getProjectDegreeList() {
    var url = dataUrl + "/basic/pro_degree/all"+ "?newDate=" + (new Date().getTime()+Math.ceil(Math.random()*10));
    $.ajax({
        url: url,
        method: "get",
        dataType: "json",
        async: true,
        cache: false,
        crossDomain: true == !(document.all),
        success: function (result) {
            var html = "";
            // html += "<option value=''>------请选择------</option>";
            if (result != null) {
                for (var i = 0; i < result.length; i++) {
                    html += "<option value='" + result[i].id + "'>" + (result[i].caption) + "</option>";
                }
                $("#proDegreeId").html(html);
            } else {
                layer.msg("获取列表信息错误，请联系管理员！");
            }
        }
    });
}

//所在地区
/*function getProjectZoneList() {
    var url = dataUrl + "/multi/zone/list";
    $.ajax({
        url: url,
        method: "get",
        dataType: "json",
        success: function (result) {
            var html = "";
            // html += "<option value=''>------请选择------</option>";
            if (result != null) {
                for (var i = 0; i < result.length; i++) {
                    html += "<option value='" + result[i].id + "'>" + (result[i].caption) + "</option>";
                }
                $("#zoneId").html(html);
            } else {
                layer.msg("获取列表信息错误，请联系管理员！");
            }
        }
    });
}*/

//审核人
function getProjectCheckerList() {
    var url = dataUrl + "/projectManage/all_person"+ "?newDate=" + (new Date().getTime()+Math.ceil(Math.random()*10));
    $.ajax({
        url: url,
        method: "get",
        dataType: "json",
        async: true,
        cache: false,
        crossDomain: true == !(document.all),
        success: function (result) {
            var html = "";
            // html += "<option value=''>------请选择------</option>";
            if (result != null) {
                for (var i = 0; i < result.length; i++) {
                    html += "<option value='" + result[i].id + "'>" + (result[i].name) + "</option>";
                }
                $("#checker").html(html);
            } else {
                layer.msg("获取列表信息错误，请联系管理员！");
            }
        }
    });
}

//性质
function getCorpProp(num) {
    var url = dataUrl + "/projectManage/getCorpProp"+ "?newDate=" + (new Date().getTime()+Math.ceil(Math.random()*10));;
    $.ajax({
        url: url,
        method: "get",
        dataType: "json",
        async: false,
        // cache: false,
        // crossDomain: true == !(document.all),
        success: function (result) {
            var html = "";
            // html += "<option value=''>------请选择------</option>";
            if (result != null) {
                for (var i = 0; i < result.length; i++) {
                    html += "<option value='" + result[i].id + "'>" + (result[i].caption) + "</option>";
                }
                $("#corpPropId" + num).html(html);
            } else {
                layer.msg("获取列表信息错误，请联系管理员！");
            }
            $("#corpPropId" + num).trigger('change');
        }
    });

}

//单位
function getCorpList(corpPropId, num) {
    var url = dataUrl + "/projectManage/findCorpListByCorpPropId?corpPropId=" + corpPropId+ "&newDate=" + (new Date().getTime()+Math.ceil(Math.random()*10));
    $.ajax({
        url: url,
        method: "get",
        dataType: "json",
        async: true,
        cache: false,
        crossDomain: true == !(document.all),
        success: function (result) {
            var html = "";
            // html += "<option value=''>------请选择------</option>";
            if (result != null) {
                for (var i = 0; i < result.length; i++) {
                    html += "<option value='" + result[i].id + "'>" + (result[i].caption) + "</option>";
                }
                $("#corpId" + num).html(html);
            } else {
                layer.msg("获取列表信息错误，请联系管理员！");
            }
        }
    });
}

function checkEps(epsId, epsName, type) {
    $("#epsText").text(epsName);
    if(epsName.length>30){
        $("#epsText").css('font-size', (10-epsName/10)+'px');
    }else if (epsName.length > 20) {
        $("#epsText").css('font-size', '10px');
    }else{
        $("#epsText").css('font-size', '15px');
    }

    //font-size: 10px
    $("#epsId").val(epsId);
    // layer.close(pageii);
}

function checkZone(zoneId, zoneName, type) {
    $("#zoneName").text(zoneName);
    $("#zoneId").val(zoneId);
    // layer.close(pageii);
}
/*

function checkedEps() {
    pageii = layer.open({
        title: ['选择EPS'],
        // title : "发布任务",
        type: 1,
        fixed: true,
        //shadeClose : true,
        area: ["700px", "80%"],
        content: $("#epsDiv"),
        btn: ['确定'],
        yes: function (index, layero) {
          /!*  var inspectPersonSpan = "";
            $('input[name="checkPerson"]:checked').each(function () {//遍历每一个名字为interest的复选框，其中选中的执行函数
                var id = $(this).val();
                inspectPersonSpan += $("#span" + id).text() + ",";//拼接检查人
                // chk_value.push($(this).val());//将选中的值添加到数组chk_value中

            });
            $("#inspectPersonSpan").text(inspectPersonSpan);*!/
            layer.close(index);
        }
    });
}

function checkedZone() {
    pageii = layer.open({
        title: ['选择地区'],
        // title : "发布任务",
        type: 1,
        fixed: true,
        //shadeClose : true,
        area: ["700px", "80%"],
        content: $("#zoneDiv"),
        btn:['确定'],
            yes:function(index,layero){
              /!*  var inspectPersonSpan="";
                $('input[name="checkPerson"]:checked').each(function(){//遍历每一个名字为interest的复选框，其中选中的执行函数
                    var id=$(this).val();
                    inspectPersonSpan+=$("#span"+id).text()+",";//拼接检查人
                    // chk_value.push($(this).val());//将选中的值添加到数组chk_value中

                });
                $("#inspectPersonSpan").text(inspectPersonSpan);*!/
                layer.close(index);
            }
    });
}*/
